﻿@using Covenant.Models.Listeners
@using Covenant.Models.Grunts
@model IEnumerable<Grunt>
@{
    Layout = "_GraphLayout";
    ViewData["Title"] = "Graph";
    IEnumerable<HttpListener> Listeners = ViewBag.Listeners;
}

@section Sheets {
    <link rel="stylesheet" href="~/css/covenant-graph.css" />
}

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3">
    <h1 class="h2">Graph</h1>
</div>

<div class="d-flex flex-wrap">
    <div id="graph-box" class="border m-1 p-0" style="background-color: #999999;"></div>
    <div class="flex-grow-1 border-left border-right border-bottom m-1 p-0">
        <div class="border-top m-0 p-0">
            <div class="m-2">
                <h3 class="h3">Legend</h3>
            </div>
            <ul style="list-style-type: none;">
                <li><img src="~/images/legend-red.png" alt="Red" height="20" width="20"/> Listener</li>
                <li><img src="~/images/legend-blue.png" alt="Blue" height="20" width="20" /> Grunt (http)</li>
                <li><img src="~/images/legend-green.png" alt="Green" height="20" width="20" /> Grunt (smb)</li>
            </ul>
        </div>
        <div class="border-top mb-1 pb-2 mt-1">
            <div class="m-2">
                <h3 class="h3">Node Information</h3>
            </div>
            <ul class="nav nav-tabs mb-3" id="graph-tab" role="tablist" hidden>
                <li class="nav-link" id="start-tab-label" data-toggle="tab" href="#start-tab" role="tab" aria-controls="start-tab" aria-selected="true">
                    Start Tab
                </li>
                @foreach (Grunt g in Model)
                {
                    <li class="nav-item">
                        <a class="nav-link" id="@g.GUID-tab" data-toggle="tab" href="#node-@g.GUID" role="tab" aria-controls="@g.GUID" aria-selected="false">
                            @g.Name
                        </a>
                    </li>
                }
                @foreach (HttpListener l in Listeners)
                {
                    <li class="nav-item">
                        <a class="nav-link" id="@l.GUID-tab" data-toggle="tab" href="#node-@l.GUID" role="tab" aria-controls="@l.GUID" aria-selected="false">
                            @l.Name
                        </a>
                    </li>
                }
            </ul>
            <div class="tab-content p-2" id="graph-tab-content">
                <div class="tab-pane fade" id="start-tab" role="tabpanel" aria-labelledby="start-tab-label">
                    <p class="text-muted">Click on a node to reveal more information.</p>
                </div>
                @foreach (Grunt g in Model)
                {
                    <div class="tab-pane fade" id="node-@g.GUID" role="tabpanel" aria-labelledby="@g.GUID-tab">
                        <h5 class="h5">Grunt</h5>
                        <div class="form-group">
                            <label asp-for="@g.Name">Name</label>
                            <form asp-controller="Grunt" asp-action="Interact" asp-route-id="@g.Id" method="get">
                                <button type="submit" class="btn btn-secondary bg-light text-dark text-left w-100">@Html.DisplayFor(model => g.Name)</button>
                            </form>
                        </div>
                        <div class="form-group">
                            <label asp-for="@g.Note"></label>
                            <input asp-for="@g.Note" value="@g.Note" class="form-control" readonly disabled>
                        </div>
                        <div class="form-group">
                            <label asp-for="@g.ImplantTemplate.CommType"></label>
                            <input asp-for="@g.ImplantTemplate.CommType" asp-items="Html.GetEnumSelectList<CommunicationType>()" class="form-control" readonly disabled>
                        </div>
                        <div class="form-group">
                            @{
                                IEnumerable<SelectListItem> childrenSelectList = g.Children.Select(C =>
                                {
                                    return new SelectListItem { Text = C, Value = C, Selected = true };
                                });
                            }
                            <label asp-for="@g.Children"></label><br />
                            <select asp-for="@g.Children" asp-items=@childrenSelectList readonly disabled class="selectpicker show-menu-arrow" multiple data-dropup-auto="false" data-selected-text-format="count > 4"></select>
                        </div>
                    </div>
                }
                @foreach (HttpListener l in Listeners)
                {
                    <div class="tab-pane fade" id="node-@l.GUID" role="tabpanel" aria-labelledby="@l.GUID-tab">
                        <h5 class="h5">Listener</h5>
                        <div class="form-group">
                            <label asp-for="@l.Name">Name</label>
                            <form asp-controller="Listener" asp-action="Interact" asp-route-id="@l.Id" method="get">
                                <button type="submit" class="btn btn-secondary bg-light text-dark text-left w-100">@Html.DisplayFor(model => l.Name)</button>
                            </form>
                        </div>
                        <div class="form-group">
                            <label asp-for="@l.Description"></label>
                            <input asp-for="@l.Description" value="@l.Description" class="form-control" readonly disabled>
                        </div>
                        <div class="form-group">
                            <label asp-for="@l.Urls[0]">Urls</label>
                            @{ var urls = string.Join(",", l.Urls); }
                            <input asp-for="@l.Urls[0]" value="@urls" class="form-control" readonly disabled>
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script src="~/js/covenant-graph.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#start-tab").tab('show');

            var connection = new signalR.HubConnectionBuilder().withUrl("/gruntHub").build();

            function displayGrunt(id, name) {
                const node = { id: id, name: name, reflexive: false, x: 50, y: 50, color: "#007BFF" };
                nodes.push(node);
                restart();
            }

            function displayListener(id, name) {
                const node = { id: id, name: name, reflexive: false, x: 50, y: 50, color: "#D52728" };
                nodes.push(node);
                restart();
            }

            function displayGruntLink(idFrom, idTo) {
                const fromNode = nodes.filter(
                    function(node){ return node.id == idFrom }
                )[0];
                const toNode = nodes.filter(
                    function(node){ return node.id == idTo }
                )[0];
                toNode.color = "#2BA02C";
                const link = { source: fromNode, target: toNode, left: true, right: false, color: "#999999" };
                links.push(link);
                restart();
            }

            function displayGruntListenerLink(listenerId, gruntId) {
                const listenerNode = nodes.filter(
                    function(node){ return node.id == listenerId }
                )[0];
                const gruntNode = nodes.filter(
                    function(node){ return node.id == gruntId }
                )[0];
                const link = { source: listenerNode, target: gruntNode, left: true, right: false, color: "#999999" };
                links.push(link);
                restart();
            }

            connection.on("ReceiveGrunt", displayGrunt);
            connection.on("ReceiveListener", displayListener);
            connection.on("ReceiveGruntLink", displayGruntLink);
            connection.on("ReceiveGruntListenerLink", displayGruntListenerLink)

            connection.start()
                .then(function () {
                    connection.invoke("GetGrunts").catch(function (err) {
                        return console.error(err.toString());
                    });
                    connection.invoke("GetListeners").catch(function (err) {
                        return console.error(err.toString());
                    });
                    connection.invoke("GetGruntLinks").catch(function (err) {
                        return console.error(err.toString());
                    });
                    connection.invoke("GetGruntListenerLinks").catch(function (err) {
                        return console.error(err.toString());
                    });
                });
        });
    </script>
}